<?php
namespace app\atxunda\controller;
use think\Controller;
use think\Db;
use think\Cache;
use think\Cookie;
use think\Request;
use app\atxunda\model\AdminLog;

class Common extends Controller
{
	public function __construct(){
      	parent::__construct();
        $admin = unserialize(cookie('admin'));
    	 	if(!$admin){
             $this->error("请先登录",'login/login','',1);
    	 	}
        $admin = json_decode(json_encode($admin), true);
        $admin_data = Db::table('admin')->where('admin_id',$admin['admin_id'])->find();
        if(!$admin_data){
          Cookie::delete('admin');
          $this->redirect('login/login');
        }
        $this->admin = $admin;
        $this->assign('admin',$admin);
        $request = Request::instance();
        AdminLog::createAt($request,$this->admin['admin_id']);
	      /*$admin_id = $admin['admin_id'];
        if(!session('admin_url') && $admin_id != 1){
            $data = Db::table('admin')->field("concat(p.controller_name,'-',p.action_name) url")->join('role_privilege rp','rp.role_id=admin.role_id')->join('privilege p','p.privilege_id=rp.privilege_id')->
                where("admin_id=$admin_id")->group("p.privilege_id")->select();

            foreach ($data as $key => $value) {
              $arr[]=$value['url'];
            }
            session('admin_url',$arr);
        }
        $arr = session('admin_url');
      	$request = \think\Request::instance();
        $str = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $request->controller())).'-'.$request->action();
      	if($request->controller()=='Index'){
        	return true;
      	}
      	if($admin_id==1){
        	return true;
      	}
      	if(in_array($str,$arr)){
        	return true;
      	}else{
        	$this->error("您没有权限",'index/index',2);
      	}*/
	 }

     public function cacheclear()
     {
        Cache::clear();
     }
}